package cxydmmszl.tmp.t12;

import cxydmmszl.common.Util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

/**
 * <li style="color: red;">Prob</li>
 * 一种怪异的节点删除方式
 * <li style="color: green;">Desc</li>
 * 链表节点值类型为 int 类型，给定一个链表中的节点 node，
 * 但不给定整个链表的头节点。如何在链表中删除 node？请实现这个函数。
 * <br/><br/>备注：<br/>
 * 保证要删除的这个节点不是链表的尾节点。
 * <li style="color: green;">Input</li>
 * 给出一个单链表的节点。
 * <li style="color: green;">Output</li>
 * 不需要返回值。
 * <li style="color: blue;">Link</li> CD157
 *
 * @author habitplus
 * @since 2021-11-20 11:27
 */
public class Main {

    static class Node {
        int val;
        Node next;

        public Node(int val) {
            this.val = val;
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        String[] ss = br.readLine().split(" ");
        int k = Integer.parseInt(br.readLine());
        Node tou = new Node(999);
        Node p = tou;
        Node[] nodes = new Node[n];
        int num;
        for (int i = 0; i < n; i++) {
            num = Integer.parseInt(ss[i]);
            nodes[i] = new Node(num);
            p.next = nodes[i];
            p = nodes[i];
        }

        deleteNode(nodes[k-1]);

        printLinkedList(tou.next);
    }

    private static void deleteNode(Node node) {
        Node pre = null;

        while (node.next != null) {
            pre = node;
            pre.val = node.next.val;
            node = node.next;
        }

        pre.next = null;
    }

    public static void printLinkedList(Node head) {
        StringBuilder sb = new StringBuilder();
        while (head != null) {
            sb.append(head.val).append(" ");
            head = head.next;
        }
        System.out.println(sb.toString());
    }
}
